package com.wmapp.connect;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.wmapp.utils.SimpleStringUtils;

/**
 * Socket request wrapper class
 * @author dengl
 *
 */
public class Request {
	private static Log log=LogFactory.getLog(Request.class);
	
	//request name
	public static final String GET_ROOM_LIST_REQ="GET_ROOM_LIST_REQ";
	public static final String STOP_ROOM_REQ="STOP_ROOM_REQ";
	public static final String PAUSE_ROOM_REQ="PAUSE_ROOM_REQ";
	public static final String RESTART_ROOM_REQ="RESTART_ROOM_REQ";
	public static final String INVITE_PARTICIPANTS_REQ="INVITE_PARTICIPANTS_REQ";
	public static final String ADD_ROOM_REQ="ADD_ROOM_REQ";
	public static final String PUT_ROOM_REQ="PUT_ROOM_REQ";
	public static final String GET_ROOM_REQ="GET_ROOM_REQ";
	public static final String GET_ROOM_STATUS_REQ="GET_ROOM_STATUS_REQ";
	public static final String CTRL_ROOM_REQ="CTRL_ROOM_REQ";
	public static final String HANGUP_CALLS_REQ="HANGUP_CALLS_REQ";
	public static final String MUTE_CALLS_REQ="MUTE_CALLS_REQ";
	public static final String GET_CALL_REQ="GET_CALL_REQ";
	public static final String INVITE_CALLS_REQ="INVITE_CALLS_REQ";
	public static final String GET_BROADCAST_REQ="GET_BROADCAST_REQ";
	public static final String GET_VERSION_REQ="GET_VERSION_REQ";
	public static final String GET_STATICS_REQ="GET_STATICS_REQ";
	
	public static final String GET_CDR_REQ="GET_CDR_REQ";
    public static final String GET_HISTORY_REQ="GET_HISTORY_REQ";

	
	public static final String GET_IPADDRESS_REQ="GET_IPADDRESS_REQ";
	public static final String PUT_IPADDRESS_REQ="PUT_IPADDRESS_REQ";
	public static final String GET_ROUTETABLE_REQ="GET_ROUTETABLE_REQ";
	public static final String GET_CONFIG_REQ="GET_CONFIG_REQ";
	
	public static final String REBOOT_REQ="REBOOT_REQ";
	
	public static final String GET_SMTP_REQ="GET_SMTP_REQ";
	public static final String PUT_SMTP_REQ="PUT_SMTP_REQ";
	
	public static final String GET_RESERVATION_LIST_REQ="GET_RESERVATION_LIST_REQ";
	public static final String ADD_RESERVATION_REQ="ADD_RESERVATION_REQ";
	public static final String PUT_RESERVATION_REQ="PUT_RESERVATION_REQ";
	public static final String DEL_RESERVATION_REQ="DEL_RESERVATION_REQ";
	public static final String GET_RESERVATION_REQ="GET_RESERVATION_REQ";
	
	public static final String PUT_CONFIG_REQ="PUT_CONFIG_REQ";
	
	public static final String ADD_ROUTETABLE_REQ="ADD_ROUTETABLE_REQ";
	public static final String DEL_ROUTETABLE_REQ="DEL_ROUTETABLE_REQ";
	
	public static final String PUT_TIME_REQ="PUT_TIME_REQ";
	
	public static final String GET_ADMIN_LIST_REQ="GET_ADMIN_LIST_REQ";
	public static final String ADD_ADMIN_REQ="ADD_ADMIN_REQ";
	public static final String DEL_ADMIN_REQ="DEL_ADMIN_REQ";
	public static final String PUT_ADMIN_REQ="PUT_ADMIN_REQ";

    public static final String GET_LINE_LIST_REQ="GET_LINE_LIST_REQ";
	public static final String ADD_LINE_REQ="ADD_LINE_REQ";
	public static final String DEL_LINE_REQ="DEL_LINE_REQ";
	public static final String PUT_LINE_REQ="PUT_LINE_REQ";
	
	public static final String ADD_GROUP_REQ="ADD_GROUP_REQ";
	public static final String DEL_USER_GROUP_REQ="DEL_USER_GROUP_REQ";
	public static final String DEL_GROUP_REQ="DEL_GROUP_REQ";
	public static final String PUT_GROUP_REQ="PUT_GROUP_REQ";
	public static final String GET_GROUP_LIST_REQ="GET_GROUP_LIST_REQ";
	
	public static final String PUT_USER_REQ="PUT_USER_REQ";
	public static final String DEL_USER_REQ="DEL_USER_REQ";
	public static final String ADD_USER_REQ="ADD_USER_REQ";
	public static final String GET_USER_LIST_REQ="GET_USER_LIST_REQ";
	
	public static final String GET_PROFILE_LIST_REQ="GET_PROFILE_LIST_REQ";
	public static final String ADD_PROFILE_REQ="ADD_PROFILE_REQ";
	public static final String PUT_PROFILE_REQ="PUT_PROFILE_REQ";
	public static final String DEL_PROFILE_REQ="DEL_PROFILE_REQ";
	
	public static final String GET_TIME_REQ="GET_TIME_REQ";

	private String name;
	private String parameterRoot;
	private Map<String,String> params;
	private String xmlParam;
	private boolean isWrapper=false;
	private Document doc = DocumentHelper.createDocument();
	
	private StringBuffer content=new StringBuffer();
	
	public Request(String name) {
		this(name,null,null,false);
	}
	
	public Request(String name,String xmlParam) {
		this.name=name;
		this.xmlParam=xmlParam;
		buildRequestFromXML();
	}
	
	public Request(String name, Map<String,String> params) {
		this(name,params,null,false);
	}
	
	public Request(String name, Map<String,String> params,String parameterRoot) {
		this(name,params,parameterRoot,false);
	}
	
	public Request(String name, Map<String,String> params,String parameterRoot,boolean isWrapper) {
		this.name=name;
		this.params=params;
		this.parameterRoot=parameterRoot;
		this.isWrapper=isWrapper;
		
		try {
			buildRequestFromMap();
		} catch (IOException e) {
			log.error(SimpleStringUtils.join("build ",name," request fail."),e);
		}
	}
	
	private void buildRequestFromMap() throws IOException{
		if(params!=null){
			Element paramRoot=null;
			
			if(parameterRoot!=null){
				if(isWrapper){
					paramRoot=doc.addElement("root").addElement(parameterRoot);
				}else{
					paramRoot=doc.addElement(parameterRoot);
				}
			}
			
			Set<String> keys=params.keySet();
			
			for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
				String key = (String)iterator.next();
				if(paramRoot==null)
					doc.addElement(key).addText(params.get(key));
				else
					paramRoot.addElement(key).addText(params.get(key));
			}
		}

        String paramstr=doc.asXML();
        
        content.append(name);
        content.append("\n");
        content.append(Connection.CONTENT_LENGTH);
        content.append(paramstr.length());
        content.append("\n");
        content.append(paramstr);
	}
	
	private void buildRequestFromXML(){
		if(xmlParam!=null){
			content.append(name);
	        content.append("\n");
	        content.append(Connection.CONTENT_LENGTH);
	        content.append(xmlParam.length());
	        content.append("\n");
	        content.append(xmlParam);
		}
	}
	
	public String getContent(){
		return content.toString();
	}

	public String getName() {
		return name;
	}
	
}
